<template>
  <!-- 货币 -->
  <div class="currency">
    <el-select
      :disabled="readonly"
      v-model="currency"
      :placeholder="$t('请选择货币')"
      @change="change"
      value-key="code"
      clearable
    >
      <el-option
        v-for="item in options"
        :key="item.code"
        :label="item.code + item.name"
        :value="item.code + item.name"
      >
      </el-option>
    </el-select>
  </div>
</template>
<script>
import nonStandardService from '@/services/nonStandardService'
import eventBus from '@/assets/utils/eventBus.js'
export default {
  name: 'currency',
  props: {
    detail: {
      type: Object,
      default: () => {
        return {}
      },
    },
    value: {},
    disabled: Boolean,
    parameter: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      options: [],
      currency: null,
      readonly: false,
      isInit: true, //初始化的时候
    }
  },
  created() {
    this.readonly = Boolean(this.$route.query.readonly) || this.disabled
    if (!this.readonly) {
      this.queryData()
    }
    if (this.value) {
      this.setData()
    }
  },
  methods: {
    queryData() {
      /**
       * code 货币：currency 结算方式： settlementMode 票款顺序：paymentTicketMode
       * ecology 生态 多式联运: TMS 仓储:WMS
       * memberType 货主:1 承运商:3 司机:4 车老板:5 仓储货主 :6
       * industry 行业 通用 :1 行业:2
       */
      let params = {
        code: 'currency',
      }
      let templateTypeCode = this.detail.templateTypeCode
      params.templateTypeCode = templateTypeCode
      switch (templateTypeCode) {
        case 'TMSHZ': //多式联运货主
          params.ecology = 'TMS'
          params.memberType = 1
          break
        case 'TMSCYS': //多式联运承运商
          params.ecology = 'TMS'
          params.memberType = 3
          break
        case 'TMSSJ': //多式联运个体司机
          params.ecology = 'TMS'
          params.memberType = 4
          break
        case 'TMSCLB': //多式联运车老板
          params.ecology = 'TMS'
          params.memberType = 5
          break
        case 'WMSHZ1': //仓储货主（通用）
          params.ecology = 'WMS'
          params.memberType = 6
          params.industry = 1
          break
        case 'WMSHZ2': //仓储货主（行业）
          params.ecology = 'WMS'
          params.memberType = 6
          params.industry = 2
          break
      }
      // 接口
      nonStandardService.basicConfiguration(params).then((res) => {
        if (res.code == 200) {
          this.options = res.data
        }
      })
    },
    getData() {
      let params = {
        value: this.currency,
      }
      return params
    },
    setData() {
      let value = this.value
      let label = value ? JSON.parse(value).value : null
      // this.currency = label ? label.indexOf('CNY') !== -1 ? 'CNY' : 'USD' : null ;
      this.currency = label ? label : null
      this.$nextTick(() => {
        this.change()
      })
    },
    change(val) {
      let params = {
        value: this.currency,
        ...this.parameter,
      }
      let obj = { [this.$t('CNY人民币')]: 1, [this.$t('USD美元')]: 2 }
      this.$emit('input', this.currency)
      eventBus.$emit('changeCurrency', this.parameter, obj[this.currency])
    },
  },
}
</script>
<style lang="scss">
.currency {
  .el-select {
    width: 100%;
  }
}
</style>
